import { asyncScheduler } from './async';
import { Observable } from './Observable';
import { Scheduler } from './Scheduler';
export function timer(dutime = 0, intervalOrShecduler: any, scheduler = asyncScheduler) {
  let intervalDuration = -1
  // 第二个参数可以传递一个调度，或者一个终止时间
  if (intervalOrShecduler != null) {
    if (intervalOrShecduler instanceof Scheduler) {
      scheduler = intervalOrShecduler
    } else {
      intervalDuration = intervalOrShecduler
    }
  }
  return new Observable(subscribe => {
    let n = 0
    return scheduler.schedule(function (this: any) {
      subscribe.next(n++)
      if (intervalDuration > 0) {
        this.schedule(undefined, intervalDuration)
      } else {
        subscribe.complete()
      }
    }, dutime)
  })
}